<template>
  <div id="login-page" >
    <div class="mask">
      <div class="mini">
        <div class="mini-close">
          <!-- <el-button
              class="close-btn"
              plain
              size="small"
              icon="el-icon-close"
              circle
              @click="closeLogin"
            ></el-button> -->
        </div>

        <div class="header">登录账号</div>

        <el-input
          placeholder="请输入用户名"
          suffix-icon="el-icon-user"
          type="username"
          v-model="userLogin.username"
          class="username"
        >
        </el-input>
        <el-input
          placeholder="请输入密码"
          v-model="userLogin.password"
          show-password
          class="password"
        ></el-input>
        <div @click="linkToRegister" class="register-text">
          未有账户？前往注册
        </div>
        <div class="button-group">
          <el-button class="login-btn" type="primary" round @click="login"
            >登录</el-button
          >
          <el-button
            class="reset-btn"
            type="default"
            native-type="reset"
            round
            @click="
              userLogin.username = '';
              userLogin.password = '';
            "
            >清空</el-button
          >
        </div>
        <div class="matemask" @click="matemaskLogin">
          <div>matemask登录</div>
          <div class="metamask-png"><img src="./images/metamask.png" /></div>
        </div>
      </div>
    </div>
    <Register />
  </div>
</template>

<script>
import { userLogin } from "@/api";
import { getMatemask } from "@/utils/matemask";
import Register from "@/components/Header/Register";
export default {
  name: "LoginPage",
  data() {
    return {
      userLogin: {
        username: "",
        password: "",
      },
    }; 
  },

  components: { Register },

  methods: {
    closeLogin() {
      this.$store.dispatch("showLogin", false);
    },
    async login() {
      let { username, password } = this.userLogin;
      // 验证输入
      if (username.trim() == "" || password.trim() == "") {
        this.$message({
          message: "请完整输入用户名和密码",
          type: "error",
          customClass: "el-message-custom",
        });
        return false;
      }

      // 验证成功，登录
      let result = await userLogin({ username, password });
      console.log(result);
      // 登录失败
      if (result.flag == false) {
        this.$message({
          message: result.msg,
          type: "error",
          customClass: "el-message-custom",
        });
        // 清空状态
        this.userLogin = { username: "", password: "" };
      }

      // 登录成功
      if (result.flag == true) {
        this.$message({
          message: result.msg,
          type: "success",
          customClass: "el-message-custom",
        });
        // 把token放到本地存储
        localStorage.setItem("token", result.token);
        // 写入用户信息到仓库
        this.$store.dispatch("userLogin", result.result[0]);
        // 关闭弹窗
        this.$store.dispatch("showLogin", false);
        // 跳转到主页
        this.$router.push("/")
      }
      // this.$store.dispatch("login", this.userLogin);
    },
    linkToRegister() {
    //   this.$store.dispatch("showLogin", false);
      this.$store.dispatch("showRegister", true);
    },

    // matemask登录
    async matemaskLogin() {
      let web3 = await getMatemask();
      console.log(web3);
      let accounts = await web3.eth.getAccounts();
      let address = accounts[0];
      let dataString = await web3.utils.utf8ToHex("Hello world");
      console.log(address, dataString);
      web3.eth
        .sign("d20e920d5be9d9d34855accb501d1987", address)
        .then(console.log);
    },
  },
};
</script>

<style lang='less'>
// 动画
.login-enter,
.login-leave-to {
  transform: translateY(100vh);
}
.login-enter-active,
.login-leave-active {
  transition: all 0.5s ease;
}

#login-page {
  .mask {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    font-size: 14px;
    z-index: 1;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
  }
  .mini {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    width: 420px;
    border-radius: 8px;
    -webkit-box-shadow: 0 0 6px rgb(0 0 0 / 10%);
    box-shadow: 0 0 6px rgb(0 0 0 / 10%);
    padding: 25px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    background-color: #fff;
    background-position: 0 100%, 100% 100%;
    background-repeat: no-repeat, no-repeat;
    background-size: 14%;
    position: relative;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    .header {
      font-size: 18px;
      color: #505050;
    }
    .mini-close {
      position: absolute;
      right: 15px;
      top: 15px;
      .close-btn:hover {
        color: var(--themeColor1);
        border: var(--themeColor1) 1px solid;
      }
      i {
        font-weight: bold;
      }
    }
    .matemask {
      cursor: pointer;
      margin-top: 20px;
      width: 250px;
      height: 40px;
      /* Permalink - use to edit and share this gradient: https://colorzilla.com/gradient-editor/#f4831f+0,f38181+100 */
      background: #f4831f; /* Old browsers */
      background: -moz-linear-gradient(
        top,
        #f4831f 0%,
        #f38181 100%
      ); /* FF3.6-15 */
      background: -webkit-linear-gradient(
        top,
        #f4831f 0%,
        #f38181 100%
      ); /* Chrome10-25,Safari5.1-6 */
      background: linear-gradient(
        to bottom,
        #f4831f 0%,
        #f38181 100%
      ); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
      filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f4831f', endColorstr='#f38181',GradientType=0 ); /* IE6-9 */

      border-radius: 40px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #fff;
      .metamask-png {
        width: 25px;
        height: 25px;
        margin-left: 10px;
        img {
          width: 100%;
        }
      }
    }
    .username {
      margin-top: 20px;
    }
    .password {
      margin-top: 20px;
    }
    .register-text {
      margin-top: 20px;
      cursor: pointer;
    }
    .register-text:hover {
      color: var(--themeColor1);
    }
    .button-group {
      margin-top: 20px;
      display: flex;
      justify-content: center;
      .login-btn {
        background: var(--themeColor1);
        border: var(--themeColor1) 1px solid;
      }
      .reset-btn:hover {
        background: var(--themeColor1);
        border: var(--themeColor1) 1px solid;
        opacity: 0.4;
        color: #fff;
      }
    }
  }
}
</style>